@{
    ViewData["Title"] = "过程检验";
    Layout = null;
}

<link href="~/layui/css/layui.css" rel="stylesheet" />
<script src="~/layui/layui.js"></script>

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>过程检验</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <style>
        .required::before {
            content: "*";
            color: red;
            margin-right: 5px;
        }
        .form-container {
            padding: 20px;
        }
        .layui-form-item {
            margin-bottom: 15px;
        }
        .btn-container {
            text-align: center;
            margin-top: 20px;
        }
    </style>
</head>
<body>
    <div class="layui-container form-container">
        <div class="layui-card">
            <div class="layui-card-header">过程检验信息</div>
            <div class="layui-card-body">
                <form class="layui-form" lay-filter="inspectForm">
                    <!-- 隐藏的ID字段 -->
                    <input type="hidden" name="id" value="@ViewBag.Task?.Id">
                    
                    <div class="layui-row">
                        <div class="layui-col-md6">
                            <div class="layui-form-item">
                                <label class="layui-form-label required">检验单编码</label>
                                <div class="layui-input-block">
                                    <input type="text" name="documentsCode" placeholder="请输入检验单编码" autocomplete="off" class="layui-input" value="@Html.Raw(ViewBag.Task?.DocumentsCode)">
                                </div>
                            </div>
                        </div>
                        <div class="layui-col-md6">
                            <div class="layui-form-item">
                                <label class="layui-form-label">检验单名称</label>
                                <div class="layui-input-block">
                                    <input type="text" name="documentsName" placeholder="请输入检验单名称" autocomplete="off" class="layui-input" value="@Html.Raw(ViewBag.Task?.DocumentsName)">
                                </div>
                            </div>
                        </div>
                    </div>
                    
                    <div class="layui-row">
                        <div class="layui-col-md6">
                            <div class="layui-form-item">
                                <label class="layui-form-label required">检验类型</label>
                                <div class="layui-input-block">
                                    <select name="documentsType" lay-verify="required">
                                        <option value="">请选择检验类型</option>
                                        <option value="入库检验">入库检验</option>
                                        <option value="出库检验">出库检验</option>
                                        <option value="过程检验" selected>过程检验</option>
                                    </select>
                                </div>
                            </div>
                        </div>
                        <div class="layui-col-md6">
                            <div class="layui-form-item">
                                <label class="layui-form-label required">工作站编码</label>
                                <div class="layui-input-block">
                                    <input type="text" name="workstationCode" placeholder="请输入工作站编码" autocomplete="off" class="layui-input" value="@Html.Raw(ViewBag.Task?.WorkstationCode)">
                                </div>
                            </div>
                        </div>
                    </div>
                    
                    <div class="layui-row">
                        <div class="layui-col-md6">
                            <div class="layui-form-item">
                                <label class="layui-form-label required">工单编码</label>
                                <div class="layui-input-block">
                                    <input type="text" name="ticketsCode" placeholder="请输入工单编码" autocomplete="off" class="layui-input" value="@Html.Raw(ViewBag.Task?.TicketsCode)">
                                </div>
                            </div>
                        </div>
                        <div class="layui-col-md6">
                            <div class="layui-form-item">
                                <label class="layui-form-label">工单名称</label>
                                <div class="layui-input-block">
                                    <input type="text" name="ticketsName" placeholder="请输入工单名称" autocomplete="off" class="layui-input" value="@Html.Raw(ViewBag.Task?.TicketsName)">
                                </div>
                            </div>
                        </div>
                    </div>
                    
                    <div class="layui-row">
                        <div class="layui-col-md6">
                            <div class="layui-form-item">
                                <label class="layui-form-label">物资编码</label>
                                <div class="layui-input-block">
                                    <input type="text" name="materialCode" placeholder="请输入物资编码" autocomplete="off" class="layui-input" value="@Html.Raw(ViewBag.Task?.MaterialCode)">
                                </div>
                            </div>
                        </div>
                        <div class="layui-col-md6">
                            <div class="layui-form-item">
                                <label class="layui-form-label">物资名称</label>
                                <div class="layui-input-block">
                                    <input type="text" name="materialName" placeholder="请输入物资名称" autocomplete="off" class="layui-input" value="@Html.Raw(ViewBag.Task?.MaterialName)">
                                </div>
                            </div>
                        </div>
                    </div>
                    
                    <div class="layui-row" style="background-color: #f8f8f8; padding: 15px; border-radius: 5px; margin-bottom: 20px;">
                        <h3 style="margin-bottom: 15px; font-weight: bold;">检验结果</h3>
                        <div class="layui-row">
                            <div class="layui-col-md4">
                                <div class="layui-form-item">
                                    <label class="layui-form-label required">待检数量</label>
                                    <div class="layui-input-block">
                                        <input type="number" name="pendingtasksNum" placeholder="请输入待检数量" autocomplete="off" class="layui-input" value="@ViewBag.Task?.PendingtasksNum" id="pendingtasksNum">
                                    </div>
                                </div>
                            </div>
                            <div class="layui-col-md4">
                                <div class="layui-form-item">
                                    <label class="layui-form-label required">合格数量</label>
                                    <div class="layui-input-block">
                                        <input type="number" name="passesNum" placeholder="请输入合格数量" autocomplete="off" class="layui-input" value="@ViewBag.Task?.PassesNum" id="passesNum">
                                    </div>
                                </div>
                            </div>
                            <div class="layui-col-md4">
                                <div class="layui-form-item">
                                    <label class="layui-form-label required">不合格数量</label>
                                    <div class="layui-input-block">
                                        <input type="number" name="noPassesNum" placeholder="不合格数量将自动计算" autocomplete="off" class="layui-input" value="@ViewBag.Task?.NoPassesNum" id="noPassesNum" readonly>
                                    </div>
                                </div>
                            </div>
                        </div>
             
                    </div>
                    
                    <div class="btn-container">
                        <button type="button" class="layui-btn layui-btn-normal" lay-submit lay-filter="saveInspection">保 存</button>
                        <button type="button" class="layui-btn layui-btn-primary" id="cancelBtn">取 消</button>
                    </div>
                </form>
            </div>
        </div>
    </div>

    <script>
        layui.use(['form', 'layer'], function(){
            var form = layui.form,
                layer = layui.layer,
                $ = layui.jquery;
            
            // 初始化显示数量
            function updateDisplayNumbers() {
                var pending = parseInt($('#pendingtasksNum').val()) || 0;
                var pass = parseInt($('#passesNum').val()) || 0;
                var fail = parseInt($('#noPassesNum').val()) || 0;
                
                $('#pendingDisplay').text(pending);
                $('#passDisplay').text(pass);
                $('#failDisplay').text(fail);
            }
            
            // 初始化时更新显示
            updateDisplayNumbers();
            
            // 当数量输入框变化时更新显示和计算不合格数量
            $('#pendingtasksNum, #passesNum').on('input', function(){
                calculateNoPassesNum();
                updateDisplayNumbers();
            });
            
            // 计算不合格数量
            function calculateNoPassesNum() {
                var pending = parseInt($('#pendingtasksNum').val()) || 0;
                var pass = parseInt($('#passesNum').val()) || 0;
                var noPass = pending - pass;
                
                // 确保不合格数量不为负
                noPass = noPass < 0 ? 0 : noPass;
                
                $('#noPassesNum').val(noPass);
            }
            
            // 自动生成检验单编号
            if (!$("input[name='documentsCode']").val()) {
                var randomCode = 'QC' + new Date().getTime().toString().substr(-8);
                $("input[name='documentsCode']").val(randomCode);
            }
            
            // 提交表单
            form.on('submit(saveInspection)', function(data){
                // 表单数据
                var formData = data.field;
                
                // 验证必填字段
                if(!formData.documentsCode) {
                    layer.msg('请输入检验单编码', {icon: 2});
                    return false;
                }
                
                if(!formData.documentsType) {
                    layer.msg('请选择检验类型', {icon: 2});
                    return false;
                }
                
                if(!formData.ticketsCode) {
                    layer.msg('请输入工单编码', {icon: 2});
                    return false;
                }
                
                if(!formData.workstationCode) {
                    layer.msg('请输入工作站编码', {icon: 2});
                    return false;
                }
                
                if(!formData.pendingtasksNum) {
                    layer.msg('请输入待检数量', {icon: 2});
                    return false;
                }
                
                if(!formData.passesNum) {
                    layer.msg('请输入合格数量', {icon: 2});
                    return false;
                }
                
                // 不合格数量由系统自动计算，无需验证
                
                // 验证数量关系
                var pending = parseInt(formData.pendingtasksNum);
                var pass = parseInt(formData.passesNum);
                var fail = parseInt(formData.noPassesNum);
                
                if(pass + fail > pending) {
                    layer.msg('合格数量与不合格数量之和不能大于待检数量', {icon: 2});
                    return false;
                }
                
                // 确保数值字段是数字类型
                formData.id = parseInt(formData.id) || 0;
                
                // 确保待检数量、合格数量和不合格数量是正确的数字
                var pendingNum = parseInt(formData.pendingtasksNum) || 0;
                var passNum = parseInt(formData.passesNum) || 0;
                var noPassNum = parseInt(formData.noPassesNum) || 0;
                
                if (pendingNum <= 0) {
                    layer.msg('待检验数量必须大于0', {icon: 2});
                    return false;
                }
                
                // 重新计算不合格数量，确保数据一致性
                noPassNum = pendingNum - passNum;
                if (noPassNum < 0) noPassNum = 0;
                
                // 确保数据是整数
                formData.pendingtasksNum = pendingNum;
                formData.passesNum = passNum;
                formData.noPassesNum = noPassNum;
                
                // 更新表单上的不合格数量显示
                $('#noPassesNum').val(noPassNum);
                
                console.log("提交的表单数据:", formData); // 调试输出
                
                // 发送保存请求
                $.ajax({
                    url: '/Pendingtasks/SaveInspection',
                    type: 'POST',
                    contentType: 'application/json',
                    data: JSON.stringify(formData),
                    success: function(res) {
                        console.log("保存结果:", res); // 调试输出
                        if(res.success) {
                            layer.msg('保存成功', {icon: 1, time: 1000}, function(){
                                // 关闭当前弹窗并刷新父页面表格
                                var index = parent.layer.getFrameIndex(window.name);
                                parent.layer.close(index);
                                parent.layui.table.reload('interviewTable');
                            });
                        } else {
                            layer.msg(res.message || '保存失败', {icon: 2});
                        }
                    },
                    error: function(xhr, status, error) {
                        console.error("保存错误:", xhr, status, error); // 调试输出
                        layer.msg('服务器错误，请稍后重试: ' + error, {icon: 2});
                    }
                });
                
                return false;
            });
            
            // 取消按钮
            $('#cancelBtn').on('click', function(){
                var index = parent.layer.getFrameIndex(window.name);
                parent.layer.close(index);
            });
            
            // 初始化表单数据
            function initFormData() {
                // 如果有任务数据，填充表单
                @if (ViewBag.Task != null)
                {
                    <text>
                    form.val('inspectForm', {
                        'id': '@ViewBag.Task.Id',
                        'documentsCode': '@Html.Raw(ViewBag.Task.DocumentsCode)',
                        'documentsName': '@Html.Raw(ViewBag.Task.DocumentsName)',
                        'documentsType': '@Html.Raw(ViewBag.Task.DocumentsType)',
                        'ticketsCode': '@Html.Raw(ViewBag.Task.TicketsCode)',
                        'ticketsName': '@Html.Raw(ViewBag.Task.TicketsName)',
                        'workstationCode': '@Html.Raw(ViewBag.Task.WorkstationCode)',
                        'materialCode': '@Html.Raw(ViewBag.Task.MaterialCode)',
                        'materialName': '@Html.Raw(ViewBag.Task.MaterialName)',
                        'pendingtasksNum': parseInt('@ViewBag.Task.PendingtasksNum') || 0,
                        'passesNum': parseInt('@ViewBag.Task.PassesNum') || 0,
                        'noPassesNum': parseInt('@ViewBag.Task.NoPassesNum') || 0
                    });
                    calculateNoPassesNum();
                    updateDisplayNumbers();
                    </text>
                }
                else
                {
                    <text>
                    // 默认生成检验单编号
                    if (!$("input[name='documentsCode']").val()) {
                        var randomCode = 'QC' + new Date().getTime().toString().substr(-8);
                        $("input[name='documentsCode']").val(randomCode);
                    }
                    </text>
                }
                
                form.render();
            }
            
            // 初始化表单
            initFormData();
        });
    </script>
</body>
</html> 